<?php

/**
 * 收货地址管理
 */
namespace app\order\api;

class AddressApi extends \app\member\api\MemberApi {

    /**
     * 收货地址列表
     */
    public function index() {
        $list = target('order/OrderAddress')->loadList([
            'user_id' => $this->userId
        ], 0, 'add_id desc');
        $this->success('ok', $list);
    }

    /**
     * 添加地址
     */
    public function add() {
        $id = target('order/Address', 'service')->add($this->userId, $this->data);
        if(!$id) {
            $this->error(target('order/Address', 'service')->getError());
        }
        $this->success('ok', ['id' => $id]);
    }

    /**
     * 编辑地址
     */
    public function edit() {
        if(!target('order/Address', 'service')->edit($this->userId, $this->data)) {
            $this->error(target('order/Address', 'service')->getError());
        }
        $this->success('ok');
    }

    /**
     * 删除地址
     */
    public function del() {
        if(!target('order/Address', 'service')->del($this->userId, $this->data['id'])) {
            $this->error(target('order/Address', 'service')->getError());
        }
        $this->success('ok');
    }

    public function default() {
        $info = target('order/Address', 'service')->getAddress($this->userId);
        if(!$info) {
            $this->error(target('order/Address', 'service')->getError());
        }
        $this->success('ok', $info);
    }


}